package com.andong.controller;

import com.andong.annotations.AccessControl;
import com.andong.domain.dto.UpdatePasswordDTO;
import com.andong.domain.entity.Coach;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.service.CoachService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.time.LocalDate;

import static com.andong.utils.SystemConstant.*;
import static com.andong.utils.SystemConstant.SUPER_ADMIN;

/**
 * 教练信息表(Coach)表控制层
 *
 * @author makejava
 * @since 2024-01-03 17:59:28
 */
@RestController
@RequestMapping("/coach")
public class CoachController {
    @Resource
    private CoachService coachService;

    @GetMapping("/{id}")
    @AccessControl(roles = {STUDENT, COACH})
    public Result selectById(@PathVariable Long id) {
        return coachService.selectById(id);
    }

    @PutMapping("/updatePassByCode")
    @AccessControl(roles = COACH)
    public Result updatePasswordByCode(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return coachService.updatePasswordByCode(dto);
    }

    @PutMapping("/updatePassByPre")
    @AccessControl(roles = COACH)
    public Result updatePasswordByPre(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return coachService.updatePasswordByPre(dto);
    }

    @GetMapping("/list")
    @AccessControl(roles = {STUDENT, ADMIN, SUPER_ADMIN}, permission = "教练管理")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender, Integer licenseType,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageResult pageResult = coachService.queryList(page, pageSize, name, gender, licenseType, begin, end);
        return Result.success(pageResult);
    }

    @PutMapping("/image")
    @AccessControl(roles = COACH)
    public Result updateImage(String newUrl) {
        coachService.updateImage(newUrl);
        return Result.success();
    }

    @PutMapping("/phone")
    @AccessControl(roles = COACH)
    public Result updatePhone(String phone, String code) {
        return coachService.updatePhone(phone, code);
    }

    @PutMapping("/status")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "教练管理")
    public Result updateStatus(Long id, Integer status) {
        return coachService.updateStatus(id, status);
    }

    @DeleteMapping("/{id}")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "教练管理")
    public Result delete(@PathVariable Long id) {
        return coachService.delete(id);
    }

    @PostMapping("/add")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "教练管理")
    public Result insert(@RequestBody Coach coach) throws Exception {
        return coachService.insert(coach);
    }
}

